package config

import (
	"io/ioutil"

	"gopkg.in/yaml.v2"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB
var TriggerChannel chan string
var ConfigChannel chan int

type _config struct {
	Workdir     string `yaml:"workdir"`
	WorkflowDir string `yaml:"workflow_dir"`
	DSN         string `yaml:"dsn"`
	LogFile     string `yaml:"log_file"`
	Banner      []byte `yaml:"-"`
}

var Config *_config

func init() {
	Config = new(_config)

	content, err := ioutil.ReadFile("workflow.yml")
	if err != nil {
		panic(err)
	}

	err = yaml.Unmarshal(content, Config)
	if err != nil {
		panic(err)
	}

	DB, err = gorm.Open(mysql.Open(Config.DSN), &gorm.Config{})
	if err != nil {
		panic(err)
	}

	TriggerChannel = make(chan string, 10)

	// 配置文件channel
	ConfigChannel = make(chan int)

	banner, err := ioutil.ReadFile("banner.txt")
	if err != nil {
		Config.Banner = []byte{}
	}
	Config.Banner = banner
}
